﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using smartphone.App_Code;

namespace smartphone.UserControls
{
    public partial class ProductDetail : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string id = Request.QueryString["ProductId"];
            if (id != null)
            {
                PopulateContol(id);
            }
        }

        /// <summary>
        /// Lay thong tin chi tiet san pham
        /// </summary>
        /// <param name="id"></param>
        private void PopulateContol(string id)
        {
            Product product = new Product();
            DataTable tableProduct = product.GetProductDetail(Int32.Parse(id));

            DataTable tableAlbum = product.GetProductAlbum(Int32.Parse(id));
            //lay ra album hinh anh dua vao slide show
            productImageItems.DataSource = tableAlbum;
            productImageItems.DataBind();

            if (tableProduct.Rows.Count > 0)
            {
                DataRow row = tableProduct.Rows[0];

                //ten san pham
                productName.InnerHtml = row["tenSanPham"].ToString();

                //gioi thieu ngan
                if (row["moTa"].ToString().Length > 120)
                    shortDescription.InnerText = row["moTa"].ToString().Substring(0, 120) + "...";
                else
                    shortDescription.InnerText = Server.HtmlDecode(row["moTa"].ToString());

                //gia san pham
                double tempMoney = Double.Parse(row["donGia"].ToString());
                productDetailPrice.InnerText = String.Format("{0:0,0}", tempMoney) + " vnđ";

                //so luong san pham hien con
                if (Int32.Parse(row["soLuong"].ToString()) > 0)
                {
                    buyBlock.InnerText = "Tình trạng: còn hàng.";
                }
                else
                {
                    buyBlock.InnerText = "Tình trạng: hết hàng.";
                }


                string strStart = String.Format(row["ngayBatDau"].ToString(), "yyyy-MM-dd");
                string strEnd = String.Format(row["ngayKetThuc"].ToString(), "yyyy-MM-dd");
                DateTime startDate = new DateTime();
                DateTime endDate = new DateTime();

                //lay ngay hien tai so san voi ngay bat dau vao ngay ket thuc xem khuyen mai co con hieu luc hay khong

                DateTime now = DateTime.Now;

                //kiem tra xem co khuyen mai hay khong vao thu thi so sanh
                if (strStart != "" && strEnd != "")
                {
                    //lay ngay bat dau khuyen mai
                    //startDate = DateTime.ParseExact(strStart, "yyyy-MM-dd", null);
                    startDate = DateTime.Parse(strStart);
                    //lay ngay ket thuc khuyen mai
                    //endDate = DateTime.ParseExact(strEnd, "yyyy-MM-dd", null);
                    endDate = DateTime.Parse(strEnd);

                    if (row["noiDungKhuyenMai"].ToString() != "" && DateTime.Compare(startDate, now) < 0 && DateTime.Compare(now, endDate) < 0)
                    {
                        //neu khuyen mai la tang qua
                        if (bool.Parse(row["giamGia"].ToString()) == false)
                        {
                            LabelPromotion.Visible = true;
                            LabelPromotion.Text = HttpUtility.HtmlDecode(row["noiDungKhuyenMai"].ToString());
                        }
                        //neu khuyen mai la giam gia
                        else
                        {
                            double discount = double.Parse(row["noiDungKhuyenMai"].ToString());
                            double newMoney = tempMoney - (tempMoney * discount) / 100;
                            LabelPromotion.Text = "Giảm " + discount.ToString() + "% còn " +
                                String.Format("{0:0,0}", newMoney) + " vnđ";
                            LabelPromotion.Visible = true;
                        }
                    }
                }

                //mo ta day du ve san pham
                productDetailDescriptionFull.InnerText = row["moTa"].ToString();

                //tab2 chua thong tin ki thuat ve san pham
                productDetailManufacturer.InnerText = "Hãng sản xuất: " + row["tenHang"].ToString();
                productDetailAccessory.InnerText = "Phụ kiện: " + row["phuKien"].ToString();
                productDetailWarranty.InnerText = "Thời gian bảo hành: " + row["thoiGianBh"].ToString() + " tháng";
                productDetailOS.InnerText = "Hệ điều hành: " + row["hdh"].ToString();
                productDetailCpu.InnerText = "Cpu: " + row["cpu"].ToString() + " Ghz";
                productDetailCamera.InnerText = "Máy ảnh: " + row["mayAnh"].ToString() + " megapixel";
                productDetailWifi.InnerText = "Wifi: " + row["wifi"].ToString();
                productDetailAudio.InnerText = "Định dạng nghe nhạc: " + row["dinhDangNhac"].ToString();
                productDetailSensor.InnerText = "Cảm ứng: " + row["camBien"].ToString();
                productDetailMovie.InnerText = "Định dạng video: " + row["dinhDangPhim"].ToString();
                productDetailRecord.InnerText = "Ghi âm: ";
                productDetailRecord.InnerText += bool.Parse(row["ghiAm"].ToString()) ? "Có" : "Không";

                //lay thong tin ve attr
                DataTable attrTable = product.GetProductAttributes(Int32.Parse(id));

                string prevAttr = "";
                string attrName, attrValue, attrValueId;

                Label attrNameLabel;
                DropDownList attrValueDropDownList = new DropDownList();

                foreach (DataRow rowAttr in attrTable.Rows)
                {
                    attrName = rowAttr["tenThuocTinh"].ToString();
                    attrValue = rowAttr["giaTriThuocTinh"].ToString();
                    attrValueId = rowAttr["maGiaTriThuocTinh"].ToString();

                    //1 san pham co the co nhieu thuoc tinh, mau sac, kich thuoc, ...
                    if (attrName != prevAttr)
                    {
                        prevAttr = attrName;
                        attrNameLabel = new Label();
                        attrNameLabel.Text = attrName;

                        attrValueDropDownList = new DropDownList();
                        PlaceHolderAttributes.Controls.Add(attrNameLabel);
                        PlaceHolderAttributes.Controls.Add(attrValueDropDownList);
                    }
                    //them cac gia tri thuoc tinh vao dropdownlist
                    attrValueDropDownList.Items.Add(new ListItem(attrValue, attrValueId));
                }

            }

        }
        /// <summary>
        /// Su kien dua san pham vao gio hang
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void LinkButtonToCart_Click(object sender, EventArgs e)
        {
            //lay ma san pham hien dang xem chi tiet
            string id = Request.QueryString["ProductId"];
            string options = "";

            //Lay gia tri cua thuoc tinh bang cach duyet qua cac control tren placeholder
            foreach (Control ctr in PlaceHolderAttributes.Controls)
            {
                //lay ten loai thuoc tinh
                if (ctr is Label)
                {
                    Label lb = (Label)ctr;
                    options += lb.Text;
                }
                //lay gia tri cua thuoc tinh
                if (ctr is DropDownList)
                {
                    DropDownList dr = (DropDownList)ctr;
                    options += dr.Items[dr.SelectedIndex] + "; ";
                }
            }

            ShoppingCart shop = new ShoppingCart();
            shop.AddItem(id, options);
        }
    }
}